package cn.exrick.xboot.modules.bicmktbidbackinfo.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.bicmktbidbackinfo.entity.BicMktBidBackInfo;
import cn.exrick.xboot.modules.bicmktbidbackinfo.service.IBicMktBidBackInfoService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sdtf
 */
@Slf4j
@RestController
@DS("other")
@Api(tags = "中标管理管理接口")
@RequestMapping("/xboot/bicMktBidBackInfo")
@Transactional
public class BicMktBidBackInfoController {

    @Autowired
    private IBicMktBidBackInfoService iBicMktBidBackInfoService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<BicMktBidBackInfo> get(@PathVariable String id) {

        BicMktBidBackInfo bicMktBidBackInfo = iBicMktBidBackInfoService.getById(id);
        return new ResultUtil<BicMktBidBackInfo>().setData(bicMktBidBackInfo);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<BicMktBidBackInfo>> getAll() {

        List<BicMktBidBackInfo> list = iBicMktBidBackInfoService.list();
        return new ResultUtil<List<BicMktBidBackInfo>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<BicMktBidBackInfo>> getByPage(PageVo page) {

        IPage<BicMktBidBackInfo> data = iBicMktBidBackInfoService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<BicMktBidBackInfo>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<BicMktBidBackInfo> saveOrUpdate(BicMktBidBackInfo bicMktBidBackInfo) {

        if (iBicMktBidBackInfoService.saveOrUpdate(bicMktBidBackInfo)) {
            return new ResultUtil<BicMktBidBackInfo>().setData(bicMktBidBackInfo);
        }
        return new ResultUtil<BicMktBidBackInfo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iBicMktBidBackInfoService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<BicMktBidBackInfo>> getByCondition(BicMktBidBackInfo bicMktBidBackInfo, SearchVo searchVo, PageVo page){

        QueryWrapper<BicMktBidBackInfo> qw = new QueryWrapper<>();
        // 统计日期相等匹配
        if (searchVo.getStartDate() != "" && searchVo.getEndDate() != "") {
            String startDate = searchVo.getStartDate();
            String endDate = searchVo.getEndDate();
            qw.between("create_date", startDate, endDate);
        }

//        //判断传来的是省内还是省外,如果传进来是一个值
//        if (bicMktBidBackInfo.getArealist() != null && bicMktBidBackInfo.getArealist().length == 1) {
//            bicMktBidBackInfo.setArea("山东省");
//            if ("省内".equals(bicMktBidBackInfo.getArealist()[0])) {
//                // area山东省内模糊搜索
//                qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getArea()), "area", bicMktBidBackInfo.getArea());
//            } else {
//                // area山东省外模糊搜索
//                qw.notLike(StrUtil.isNotBlank(bicMktBidBackInfo.getArea()), "area", bicMktBidBackInfo.getArea());
//            }
//        } else {
//            // area模糊搜素
//            qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getArea()), "area", bicMktBidBackInfo.getArea());
//        }

        // bidBackId模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getBidBackId()), "bid_back_id", bicMktBidBackInfo.getBidBackId());
        // biddingId模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getBiddingId()), "bidding_id", bicMktBidBackInfo.getBiddingId());
        // createId模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getCreateId()), "create_id", bicMktBidBackInfo.getCreateId());
        // createDate模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getCreateDate()), "create_date", bicMktBidBackInfo.getCreateDate());
        // modifyId模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getModifyId()), "modify_id", bicMktBidBackInfo.getModifyId());
        // modifyDate模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getModifyDate()), "modify_date", bicMktBidBackInfo.getModifyDate());
        // reiceMode模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getReiceMode()), "reice_mode", bicMktBidBackInfo.getReiceMode());
        // reiceAmount模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getReiceAmount()), "reice_amount", bicMktBidBackInfo.getReiceAmount());
        // ind模糊搜素
        qw.eq( "ind", "1");
        // remark模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getRemark()), "remark", bicMktBidBackInfo.getRemark());
        // operateAmount模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getOperateAmount()), "operate_amount", bicMktBidBackInfo.getOperateAmount());
        // status模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getStatus()), "status", bicMktBidBackInfo.getStatus());
        // bidnumber模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getBidnumber()), "bidnumber", bicMktBidBackInfo.getBidnumber());
        // bidOpenDate模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getBidOpenDate()), "bid_open_date", bicMktBidBackInfo.getBidOpenDate());
        // success模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getSuccess()), "success", bicMktBidBackInfo.getSuccess());
        // updateTime模糊搜素
        qw.like(StrUtil.isNotBlank(bicMktBidBackInfo.getUpdateTime()), "update_time", bicMktBidBackInfo.getUpdateTime());
        IPage<BicMktBidBackInfo> data = iBicMktBidBackInfoService.page(PageUtil.initMpPage(page), qw);
       List<BicMktBidBackInfo> list =  iBicMktBidBackInfoService.selectInfo();
        List<BicMktBidBackInfo> records = data.getRecords();
        for (BicMktBidBackInfo record : records) {
            for (BicMktBidBackInfo mktBidBackInfo : list) {
                if (mktBidBackInfo.getBiddingId().equals(record.getBiddingId())){
                    record.setBiddingName(mktBidBackInfo.getBiddingName());
                    record.setChargeOrgName(mktBidBackInfo.getChargeOrgName());
                    record.setProfessional(mktBidBackInfo.getProfessional());
                    record.setArea(mktBidBackInfo.getArea());
                    record.setFullname(mktBidBackInfo.getFullname());
                }
            }
        }
        return new ResultUtil<IPage<BicMktBidBackInfo>>().setData(data);
    }
}
